<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品配置</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form ref="searchFormRef" :inline="true" :model="queryInfo.query">
        <el-form-item label="一级分类" prop="firstClass">
          <el-select
            v-model="queryInfo.query.firstClass"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
            @change="changeSelect"
          >
            <el-option
              v-for="item in selectData.product_type"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" prop="secondClass">
          <el-select
            v-model="queryInfo.query.secondClass"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in secondClasses"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="type">
          <el-select
            v-model="queryInfo.query.type"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option key="1" label="服务产品" value="1"> </el-option>
            <el-option key="2" label="实物产品" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称【ID】" prop="id">
          <el-select
            v-model="queryInfo.query.id"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in selectData.product"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/product/productAdd')"
        >
          新增产品
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="productList" border stripe max-height="500px">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column
          label="产品名称【ID】"
          min-width="220px"
          max-width="500px"
        >
          <template slot-scope="scope">
            {{ scope.row.name + '【' + scope.row.id + '】' }}
          </template>
        </el-table-column>
        <el-table-column
          label="一级分类"
          prop="p_type_name.pid_info.name"
          min-width="100px"
        ></el-table-column>
        <el-table-column
          label="二级分类"
          prop="p_type_name.name"
          min-width="100px"
        ></el-table-column>
        <el-table-column label="产品类型" prop="type.name"></el-table-column>
        <el-table-column
          label="产品标准"
          prop="standard"
          min-width="220px"
          max-width="500px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="产品规格"
          prop="specs"
          min-width="100px"
        ></el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          min-width="100px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-link type="danger" @click="removeProductById(scope.row.id)"
              >删除</el-link
            >
            &nbsp;
            <!-- 修改按钮 -->
            <el-link
              type="primary"
              @click="
                $router.push({
                  path: '/product/productEdit',
                  query: { id: scope.row.id }
                })
              "
              >编辑</el-link
            >
            &nbsp;
            <!-- 配置按钮 -->
            <el-link
              type="success"
              @click="
                $router.push({
                  name: 'source',
                  query: scope.row
                })
              "
              >产品配置来源</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取产品分类列表的参数对象
      queryInfo: {
        query: {
          id: '',
          firstClass: '',
          secondClass: ''
        },
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 20
      },
      productList: [],
      total: 0,
      selectData: [],
      secondClasses: [],
      allProducts: []
    }
  },
  created() {
    this.getListData()
    this.getSelectData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('admin/Data/find', {
        level: 1,
        code: 'product_type,product'
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    // 下拉框选中事件
    async changeSelect() {
      this.secondClasses = []
      this.queryInfo.query.secondClass = ''
      var pid = this.queryInfo.query.firstClass
      const { data: res } = await this.$http.post('admin/Data/find', {
        level: 2,
        code: 'product_type',
        pid: pid
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.secondClasses = res.data.product_type
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/Product/productList',
        this.queryInfo
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.productList = res.data.list
      this.total = res.data.total
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    editData(Info) {},
    // 根据id删除对应的产品信息
    async removeProductById(id) {
      // 弹框询问是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该产品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.post('admin/Product/delProduct', {
        product_id: id
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
      this.getSelectData()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
